package com.ebelter.temperaturegun.ui.activity;




import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.http.TGNetUtils;

public class ChangePasswordActivity2 extends TBaseActivity {


    public static final String TAG = "ChangePasswordActivity2";
    public static final String EMAIL = "EMAIL";
    public static final String EMAILCODE = "EMAILCODE";

    private ImageView top_iv;
    private TextView top_tv1;
    private EditText et_pwd_changepsw2;
    private EditText et_repwd_changepsw2;


    private String emailStr;
    private String emailCodeStr;

    @Override
    protected int setContentViewId() {
        return R.layout.change_password2;
    }


    @Override
    protected void initData() {
        Bundle bundle = getBundle();
        emailStr = bundle.getString(EMAIL);
        emailCodeStr = bundle.getString(EMAILCODE);
    }

    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv1.setText(R.string.xgmm);
        ViewUtils.displayView(top_tv1);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_pwd_changepsw2=findViewById(R.id.et_pwd_changepsw2);
        et_repwd_changepsw2=findViewById(R.id.et_repwd_changepsw2);
    }

    public void complete(View view) {
        String password = et_pwd_changepsw2.getText().toString().trim();
        String password2 = et_repwd_changepsw2.getText().toString().trim();
        if (TextUtils.isEmpty(password)){
            ToastUtil.show(R.string.The_Password_cannot_be_empty);
            return;
        }

        if (password.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (password.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!TextUtils.equals(password2,password)){
            ToastUtil.show(R.string.The_password_is_inconsistency_twice);//前后密码输入不一致
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        showLoadingDialog("");
        TGNetUtils.getInstance().findPassWord(this, emailStr, MD5Utils.getMd5(password), emailCodeStr, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse,String s) {
                dissLoadingDialog();
                if (isRequestSuccess){
                    if (baseResponse.resultCode==0){
                        ToastUtil.show(R.string.Modify_Success);
                        ActivityManagers.getInstance().closeActivity(ChangePasswordActivity1.TAG);
                        ActivityManagers.getInstance().closeActivity(ChangePasswordActivity2.TAG);
                        finish();
                    }else if (baseResponse.resultCode==117){
                        ToastUtil.show(R.string.Email_is_not_registered);
                    }else if (baseResponse.resultCode==106){
                        ToastUtil.show(R.string.Email_verification_code_has_expired);

                    }else if (baseResponse.resultCode==107){
                        ToastUtil.show(R.string.Wrong_email_verification_code);
                    }else if(baseResponse.resultCode==110){
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });

    }
}
